 package com.globant.google.mendoza;

import java.util.Properties;

import junit.framework.TestCase;

import com.globant.google.mendoza.malbec.GBuy;
import com.globant.google.mendoza.malbec.SeleniumBuyerRobotFactory;
import com.globant.google.mendoza.malbec.XmlCartEncoder;
import com.globant.google.mendoza.malbec.transport.StandAlone;

public final class MendozaTest extends TestCase {

  private boolean environmentStarted = false;
  MendozaServer mendozaServer;

  protected void setUp() throws Exception {
    if (!environmentStarted) {
      start();
    }
  }

  public void start() {
    SeleniumBuyerRobotFactory robotFactory =
      new SeleniumBuyerRobotFactoryMock(MendozaTestConstants.sampleURL,
          new XmlCartEncoder(), MendozaTestConstants.sampleDirectory,
          MendozaTestConstants.sampleURL,
          MendozaTestConstants.samplePortNumber);

    mendozaServer = new MendozaServer(
        robotFactory, MendozaTestConstants.merchantId,
        MendozaTestConstants.merchantKey,
        MendozaTestConstants.calculationsTimeout);
    StandAlone transport = new StandAlone(null, null,
        MendozaTestConstants.merchantId, MendozaTestConstants.merchantKey,
        MendozaTestConstants.merchantURL, MendozaTestConstants.callbackPort,
        true);
    TransportProxy proxy = new TransportProxy(transport, mendozaServer,
        MendozaTestConstants.merchantURL, MendozaTestConstants.sampleURL);

    GBuy gbuy = new GBuy(proxy);
    gbuy.registerOrderListener(proxy);
    gbuy.start();

    /* The server starts to listen...*/
    new MendozaServerTransport(
        mendozaServer, MendozaTestConstants.mendozaServerPort);
    new GoogleCheckoutMock();
    new MerchantApplicationMock();
  }

  public void testPlaceOrder() {
    // Issue a checkout command to mendoza.
    Properties checkoutCmdProperties = new Properties();
    checkoutCmdProperties.put("cart", MendozaTestConstants.cart);
    MendozaRequest mzaRequestCheckout =
      new MendozaRequest(MendozaTestConstants.mzaCheckoutCommand,
          "", new Properties(), checkoutCmdProperties, "");
    mendozaServer.checkout(mzaRequestCheckout);

    // Issue a login command to mendoza.
    Properties loginCmdProperties = new Properties();
    loginCmdProperties.put("email", MendozaTestConstants.buyerEmail);
    loginCmdProperties.put("password", MendozaTestConstants.buyerPassword);
    MendozaRequest mzaRequestLogin =
      new MendozaRequest(MendozaTestConstants.mzaLoginCommand,
          "", new Properties(), loginCmdProperties, "");
    mendozaServer.login(mzaRequestLogin);

    // Issue a placeorder command to mendoza.
    MendozaRequest mzaRequestPlaceOrder =
      new MendozaRequest(MendozaTestConstants.mzaPlaceOrderCommand,
          "", new Properties(), new Properties(), "");
    mendozaServer.placeOrder(mzaRequestPlaceOrder);
    environmentStarted = true;
    System.out.println(mendozaServer.getMendozaStatus());
    assertEquals(MendozaTestConstants.googleCheckoutOrder,
        mendozaServer.getMendozaStatus().getGoogleOrderNumber());
    // Time to get the notifications
    try {
      Thread.sleep(4000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    //  to print updated results
    System.out.println(mendozaServer.getMendozaStatus());
    assertTrue(mendozaServer.getMendozaStatus().getNewOrderNotification()
        != null);
    assertTrue(mendozaServer.getMendozaStatus().getRiskInformationNotification()
        != null);
    assertTrue(mendozaServer.getMendozaStatus().getFinancialStates()
        != null);
    assertTrue(mendozaServer.getMendozaStatus().getFulfillmentStates()
        != null);
  }
}
